$(function(){
	//页面动态加载
	publishViewShow();
	
	//底部选项栏样式修改
	$('footer a:eq(1)').attr('class', 'currentModule');
	
	//点击发布
	$('#publishBtn').on('click', publishInfo);
});

function publishViewShow(){
	//发送请求
	$.ajax({
		type: "POST",
		dataType: 'json',
		url: global.ctx + global.publishViewUrl,
		data: {},
		success: function(json) {
			console.log(json);
			var houseArr = [], otherArr = [];
			if(json.status == 'true') {
				$('#house').empty();
				$('#other').empty();
				for(var i = 0; i < json.data.length; i++) {
					if(json.data[i].category == 1) {
						houseArr.push(json.data[i]);
					} else {
						otherArr.push(json.data[i]);
					}
				}
				
				//房产信息
				for(var j = 0; j < 6; j++) {
					var houseArrLength = houseArr.length;
					if(j < houseArrLength) {
						$('#house').append('<li data-id="' + houseArr[j].id + '">' + houseArr[j].typeName + '</li>');
					} else {
						$('#house').append('<li></li>');
					}
				}
				//其他信息
				for(var k = 0; k < 6; k++) {
					var otherArrLength = otherArr.length
					if(k < otherArrLength) {
						$('#other').append('<li data-id="' + otherArr[k].id + '">' + otherArr[k].typeName + '</li>');
					} else {
						$('#other').append('<li></li>');
					}
				}
				
				// 绑定跳转提交信息页面
				$('.info-module-content li').on('click', function() {
					if($(this).attr('data-id')){
						global.infoId = $(this).attr('data-id');
						$('#publish_moduleView_js').hide();
						$('#topNav_js').append('<span>&gt;</span><span>填写信息</span>');
						$('#topNav_js span:eq(0)').on('click', backToPublishView);
						$('#topNav_js span:eq(2)').on('click', backToPublishView);
						$('#publish_writeView_js').fadeIn();
					}
				});
			}
		},
		error: function() {
			
		}
	});
}

//返回发布页面
function backToPublishView(){
	$('#publish_writeView_js').hide();
	$('#topNav_js span:eq(3)').remove();
	$('#topNav_js span:eq(3)').remove();
	$('#publish_moduleView_js').fadeIn();
	//解除返回绑定
	$('#topNav_js span:eq(0)').off('click');
	$('#topNav_js span:eq(2)').off('click');
}

//发布信息
function publishInfo(){
	var infoArea = $('#info-area option:selected').val();
	var infoLocation = $('#info-location').val();
	var infoTit = $('#info-tit').val();
	var infoDes = $('#info-des').val();
	var infoContact = $('#info-contact').val();
	var infoTel = $('#info-tel').val();
	
	// 地址栏是否输入判断
	if(infoLocation.length == 0) {
		alert('地址不能为空');
		return;
	}
	
	// 标题字数判断
	if(infoTit.length > 15) {
		alert('标题字数大于15个字，请重新输入');
		return;
	} else if(infoTit.length < 5) {
		alert('标题字数小于5个字，请重新输入');
		return;
	}
	
	// 描述字数判断
	if(infoDes.length > 30) {
		alert('描述内容大于30个字，请重新输入');
		return;
	} else if(infoDes.length == 0) {
		alert('描述内容不能为空');
		return;
	}
	
	// 联系人
	if(infoContact.length > 10) {
		alert('联系人姓名大于6个字，请重新输入');
		return;
	} else if(infoContact.length < 2) {
		alert('联系人姓名小于2个字，请重新输入');
		return;
	} 
	
	// 手机号
	if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(infoTel))) {
		alert('请输入格式正确的手机号');
		return;
	}	
	
	//发送请求
	$.ajax({
		type: "POST",
		dataType: 'json',
		url: global.ctx + global.publishInfoUrl,
		data: {
			districtId: infoArea,
			address: infoLocation,
			title: infoTit,
			discription: infoDes,
			contactName: infoContact,
			contactPhone: infoTel,
			serveTypeId: global.infoId
		},
		success: function(json) {
			console.log(json);
			if(json.status == 'true') {
				//返回发布页面
				backToPublishView();
			}
		},
		error: function() {
			
		}
	});
}

